package cn.edu.ldu.modules.controller;

import cn.edu.ldu.modules.entity.FriendsEntity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import cn.edu.ldu.modules.entity.UserEntity;
import cn.edu.ldu.http.rqtUtils;
import cn.edu.ldu.util.Consts;

public class AppUserController extends BaseController {

    public static UserEntity getUser(Long userId, String token) throws Exception {

        JSONObject jsonObject = rqtUtils.httpclientGet(new StringBuffer(Consts.findUserById).append("/").append(userId), token);
        if(jsonObject.getString("msg").equals("success") && jsonObject.getString("user") != null){
            return  JSON.parseObject(jsonObject.getJSONObject("user").toJSONString(), UserEntity.class);
        }
        return null;
    }

    public static String setUserInfo(UserEntity user, String token) throws Exception {
        String jsonString = JSONObject.toJSONString(user);

        String responseResult = rqtUtils.httpclientPost(new StringBuffer(Consts.setUserInfo), jsonString, token);

        return JSON.parseObject(responseResult).getString("id");
    }

    public static String editPassword(UserEntity userEntity) throws Exception {

        String jsonString = JSONObject.toJSONString(userEntity);

        String responseResult = rqtUtils.httpclientPost(new StringBuffer(Consts.editPassword), jsonString, null);

        return JSONObject.parseObject(responseResult).getString("msg");
    }

    public static String deleteFriend(FriendsEntity friendsEntity, String token) throws Exception {

        String jsonString = JSONObject.toJSONString(friendsEntity);

        String responseResult = rqtUtils.httpclientPost(new StringBuffer(Consts.deleteFriend), jsonString, token);

        return JSON.parseObject(responseResult).getString("msg");

    }

}
